/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-8-25 下午3:09 Created by lyyang
 */
package com.jade.bss.web.interceptor;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.logger.ControllerLogger;
import com.jade.journal.JournalUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 控制器日志拦截器
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-8-25 下午3:09
 */
public class ControllerLoggerInterceptor extends HandlerInterceptorAdapter
{
    private Log logger = LogFactory.getLog(this.getClass());

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception
    {
        if (handler instanceof HandlerMethod) {
            Method method = ((HandlerMethod)handler).getMethod();
            String action = ((HandlerMethod)handler).getBean().getClass().getName() + "." + method.getName();
            String ip = request.getRemoteAddr();
            String params = "";
            for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
                String key = (String)en.nextElement();
                params += key + "=" + request.getParameter(key) + ";";
            }
            ControllerLogger log = new ControllerLogger();
            log.setAction(action);
            log.setIp(ip);
            log.setCreationTime(new Date());
            log.setParams(params);
            Subject subject = SecurityUtils.getSubject();
            if (subject.isAuthenticated()) {
                Object obj = subject.getPrincipal();
                if (obj instanceof Customer) {
                    log.setOperator(((Customer)obj).getName());
                    log.setOperatorId(((Customer)obj).getId());
                    log.setOperatorType("customer");
                }
                else {
                    if (obj instanceof Customer) {
                        log.setOperator(((Customer)obj).getName());
                        log.setOperatorId(((Customer)obj).getId());
                        log.setOperatorType("admin");
                    }
                    else {
                        log.setOperator(obj.getClass().getName());
                        log.setOperatorType("unknown");
                    }
                }
            }
            else {
                log.setOperator("anonymous");
                log.setOperatorId(0);
                log.setOperatorType("anonymous");

            }

            request.setAttribute("controller_logger",log);
        }
        return true;
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception
    {
        ControllerLogger log = (ControllerLogger)request.getAttribute("controller_logger");
        log.setResult(ex == null ? ControllerLogger.RESULT_SUCCESS : ControllerLogger.RESULT_FAIL);
        JournalUtils.getJournal(ControllerLogger.JOURNAL_NAME).infoEntity(log);
    }

}
